{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:37:45.952972Z",
     "start_time": "2020-06-14T07:37:45.331064Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:37:59.291581Z",
     "start_time": "2020-06-14T07:37:59.285443Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.1210e-44, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.empty(5,3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:38:23.078058Z",
     "start_time": "2020-06-14T07:38:23.066616Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.9248, 0.7832, 0.7199],\n",
       "        [0.4648, 0.5422, 0.9727],\n",
       "        [0.6847, 0.9988, 0.0836],\n",
       "        [0.4807, 0.8104, 0.3448],\n",
       "        [0.3133, 0.0873, 0.3528]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.rand(5,3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:38:44.404305Z",
     "start_time": "2020-06-14T07:38:44.398496Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 0, 0],\n",
       "        [0, 0, 0],\n",
       "        [0, 0, 0],\n",
       "        [0, 0, 0],\n",
       "        [0, 0, 0]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.zeros(5,3,dtype=torch.long)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:39:04.245870Z",
     "start_time": "2020-06-14T07:39:04.239998Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([5.5000, 3.0000])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([5.5,3])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:39:40.126031Z",
     "start_time": "2020-06-14T07:39:40.119920Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [1., 1., 1.],\n",
       "        [1., 1., 1.],\n",
       "        [1., 1., 1.],\n",
       "        [1., 1., 1.]], dtype=torch.float64)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = x.new_ones(5,3,dtype=torch.float64)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:40:09.615790Z",
     "start_time": "2020-06-14T07:40:09.610002Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-2.1915,  1.0952, -0.5077],\n",
       "        [ 0.6870,  0.0576,  1.5716],\n",
       "        [-1.5767,  0.8100,  0.7129],\n",
       "        [-0.3312,  1.0468,  0.9813],\n",
       "        [-0.9653,  0.2766,  1.2864]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.randn_like(x,dtype=torch.float)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:40:53.856736Z",
     "start_time": "2020-06-14T07:40:53.852090Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([5, 3]), torch.Size([5, 3]))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.size(),x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:44:10.917131Z",
     "start_time": "2020-06-14T07:44:10.911659Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.9239,  1.9926, -0.0310],\n",
       "        [ 1.2558,  0.0975,  1.8677],\n",
       "        [-0.8616,  1.6279,  1.2961],\n",
       "        [-0.2501,  1.8888,  1.8400],\n",
       "        [-0.2871,  0.3252,  1.5818]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = torch.rand(5,3)\n",
    "x+y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:44:25.751905Z",
     "start_time": "2020-06-14T07:44:25.746349Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.9239,  1.9926, -0.0310],\n",
       "        [ 1.2558,  0.0975,  1.8677],\n",
       "        [-0.8616,  1.6279,  1.2961],\n",
       "        [-0.2501,  1.8888,  1.8400],\n",
       "        [-0.2871,  0.3252,  1.5818]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.add(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:45:34.106664Z",
     "start_time": "2020-06-14T07:45:34.100988Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.9239,  1.9926, -0.0310],\n",
       "        [ 1.2558,  0.0975,  1.8677],\n",
       "        [-0.8616,  1.6279,  1.2961],\n",
       "        [-0.2501,  1.8888,  1.8400],\n",
       "        [-0.2871,  0.3252,  1.5818]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = torch.empty(5,3)\n",
    "torch.add(x,y,out=result)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:46:39.638031Z",
     "start_time": "2020-06-14T07:46:39.632469Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.9239,  1.9926, -0.0310],\n",
       "        [ 1.2558,  0.0975,  1.8677],\n",
       "        [-0.8616,  1.6279,  1.2961],\n",
       "        [-0.2501,  1.8888,  1.8400],\n",
       "        [-0.2871,  0.3252,  1.5818]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.add_(x)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:47:02.938660Z",
     "start_time": "2020-06-14T07:47:02.935444Z"
    }
   },
   "outputs": [],
   "source": [
    "y = x[0,:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:47:07.664267Z",
     "start_time": "2020-06-14T07:47:07.661299Z"
    }
   },
   "outputs": [],
   "source": [
    "y += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:47:10.101406Z",
     "start_time": "2020-06-14T07:47:10.096876Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-1.1915,  2.0952,  0.4923])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:48:34.068004Z",
     "start_time": "2020-06-14T07:48:34.062716Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.1915,  2.0952,  0.4923],\n",
       "        [ 0.6870,  0.0576,  1.5716],\n",
       "        [-1.5767,  0.8100,  0.7129],\n",
       "        [-0.3312,  1.0468,  0.9813],\n",
       "        [-0.9653,  0.2766,  1.2864]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:49:02.009048Z",
     "start_time": "2020-06-14T07:49:02.003422Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([5, 3]), torch.Size([15]), torch.Size([3, 5]))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = x.view(15)\n",
    "z = x.view(-1,5)\n",
    "x.size(),y.size(),z.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:49:29.937763Z",
     "start_time": "2020-06-14T07:49:29.932238Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-1.1915,  2.0952,  0.4923,  0.6870,  0.0576,  1.5716, -1.5767,  0.8100,\n",
       "         0.7129, -0.3312,  1.0468,  0.9813, -0.9653,  0.2766,  1.2864])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:50:41.551253Z",
     "start_time": "2020-06-14T07:50:41.544800Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-2.1915,  1.0952, -0.5077],\n",
       "        [-0.3130, -0.9424,  0.5716],\n",
       "        [-2.5767, -0.1900, -0.2871],\n",
       "        [-1.3312,  0.0468, -0.0187],\n",
       "        [-1.9653, -0.7234,  0.2864]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_cp = x.clone().view(15)\n",
    "x -=1\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:50:43.111711Z",
     "start_time": "2020-06-14T07:50:43.107056Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-1.1915,  2.0952,  0.4923,  0.6870,  0.0576,  1.5716, -1.5767,  0.8100,\n",
       "         0.7129, -0.3312,  1.0468,  0.9813, -0.9653,  0.2766,  1.2864])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_cp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:51:15.832270Z",
     "start_time": "2020-06-14T07:51:15.827329Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.05405539646744728"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.randn(1)\n",
    "x.item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:52:52.494611Z",
     "start_time": "2020-06-14T07:52:52.489341Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(1,3).view(1,2)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:53:17.576001Z",
     "start_time": "2020-06-14T07:53:17.572178Z"
    }
   },
   "outputs": [],
   "source": [
    "y = torch.arange(1,4).view(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:53:17.956956Z",
     "start_time": "2020-06-14T07:53:17.952142Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1],\n",
       "        [2],\n",
       "        [3]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:54:27.682829Z",
     "start_time": "2020-06-14T07:54:27.676685Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([1,2])\n",
    "y = torch.tensor([3,4])\n",
    "id_before = id(y)\n",
    "y = y+x\n",
    "id(y) == id_before"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:54:47.200354Z",
     "start_time": "2020-06-14T07:54:47.194204Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([1,2])\n",
    "y = torch.tensor([3,4])\n",
    "id_before = id(y)\n",
    "y[:] = y+x\n",
    "id(y) == id_before"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:55:48.679889Z",
     "start_time": "2020-06-14T07:55:48.674536Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1.], dtype=float32)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.ones(5)\n",
    "b = a.numpy()\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:55:50.114235Z",
     "start_time": "2020-06-14T07:55:50.109155Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:56:09.197226Z",
     "start_time": "2020-06-14T07:56:09.194370Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-14T07:56:26.432433Z",
     "start_time": "2020-06-14T07:56:26.426420Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1., 1., 1., 1., 1.]),\n",
       " tensor([1., 1., 1., 1., 1.], dtype=torch.float64))"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.ones(5)\n",
    "b = torch.from_numpy(a)\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = torch.ones(2,2,requires_)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.5 64-bit ('numpy': virtualenv)",
   "language": "python",
   "name": "python37564bitnumpyvirtualenv325e501f273d447b90cac77d0bfa67d7"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
